package com.biz.primus.ms.tms.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.model.tms.enums.DeliveryOrderStatus;
import com.biz.primus.ms.tms.dao.ro.DeliveryOrderRo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * DeliveryOrderRedisDao
 *
 * @author JKLiues
 * @date 2017年11月27日
 */
@Repository
@Slf4j
public class DeliveryOrderRedisDao extends CrudRedisDao<DeliveryOrderRo, Long> {


    public Integer getValue(String empCode, DeliveryOrderStatus status) {
        String key = this.getOrderNumLKey(empCode, status);
        byte[] bytes = get(key);
        Integer value = RedisUtil.byteArrayToInt(bytes);
        return value == null ? 0 : value;
    }

    public void incrValue(String empCode, DeliveryOrderStatus status) {
        String key = this.getOrderNumLKey(empCode, status);
        Long incr = super.incr(key);
    }

    public void decrValue(String empCode, DeliveryOrderStatus status) {
        String key = this.getOrderNumLKey(empCode, status);
        Long decr = super.decr(key);
        if (decr < 0L) {
            log.error("{}配送单{}状态减少至0", empCode, status);
            super.set(key, RedisUtil.toByteArray(0L));
        }
    }


    private String getOrderNumLKey(String empCode, DeliveryOrderStatus status) {
        return super.getKeyByParams(empCode, status);
    }
}